//
//  ViewController.m
//  14collection练习
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "ImageCollectionViewCell.h"
#import "LabelCollectionReusableView.h"
#import "LabelAndCollectionViewCollectionReusableView.h"
#define kArcRandomn [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) NSMutableArray *imageArray;
@property (nonatomic,strong) UIImageView *myImageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
//    NSArray *array = @[@"0",@"1",@"2",@"3"];
//    for (int i = 0; i < 4; i++) {
//        _myImageView.image = [UIImage imageNamed:array[i]];
//        [_imageArray addObject:_myImageView];
//    }

    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    [self.view addSubview:myCollectionView];
    
    [myCollectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
//    [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
    
    CGFloat width = (CGRectGetWidth(self.view.bounds) - 10) / 3;
    layout.itemSize = CGSizeMake(width, width);
//    列间距
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 5;
    
    layout.headerReferenceSize = CGSizeMake(0, 40);
    layout.sectionHeadersPinToVisibleBounds = YES;
    
    [myCollectionView registerClass:[LabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuse"];
    
    
//    1通过上边距实现视图添加（本质是UIScrollView）
    myCollectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -295, self.view.bounds.size.width, 290)];
    view.backgroundColor = [UIColor greenColor];
    [myCollectionView addSubview:view];
    
//    2自定义头视图
    [myCollectionView registerClass:[LabelAndCollectionViewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuse1"];
}





- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        LabelAndCollectionViewCollectionReusableView *reuse1 =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuse1" forIndexPath:indexPath];

//        reuse1.backgroundColor = [UIColor whiteColor];
        return reuse1;
    }else{
        
   LabelCollectionReusableView *reuse = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuse" forIndexPath:indexPath];
    reuse.backgroundColor = [UIColor redColor];
    return reuse;
   }
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = kArcRandomn;
//    [cell.contentView addSubview:_imageArray[indexPath.row]];
    
//    cell.contentView
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
